#include "System.h"

using namespace std;

void System::searchPlace()
{
	setbkcolor(RGB(255, 255, 255));
	cleardevice();
	//顶栏设置
	setfillcolor(RGB(130, 226, 255));
	fillroundrect(0, 0, 375, 83, 0, 0);
	settextcolor(RGB(0, 72, 125));
	settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
	setbkmode(TRANSPARENT);//去除文字背景
	outtextxy(151, 20, "景点");
	IMAGE retn;
	loadimage(&retn, "./返回.png", 16, 29);
	putimage(25, 27, &retn);

	//搜索框
	Textbox tb1(17, 102, 282, 57, "", 58);
	tb1.settitle("请输入待搜索的景点名:");
	tb1.show();
	IMAGE fangdajing;
	loadimage(&fangdajing, "./放大镜.png", 32, 32);
	putimage(25, 115, &fangdajing);
	setlinecolor(RGB(0, 0, 0));
	line(31, 160, 286, 160);

	BasicButton btn1(301, 112, 63, 38, 0, "查询");
	btn1.setbtncl(96, 218, 255);
	btn1.settext(0, 72, 125, 24, 309, 117);
	btn1.show();

	line(25, 342, 349, 342);
	line(25, 524, 349, 524);

	//下栏
	IMAGE frontPage;
	loadimage(&frontPage, "./箭头_列表向左.png", 26, 41);
	putimage(64, 731, &frontPage);

	IMAGE backPage;
	loadimage(&backPage, "./箭头_列表向右.png", 26, 41);
	putimage(282, 731, &backPage);

	IMAGE img;
	loadimage(&img, "./景点.png", 44, 44);
	putimage(165, 714, &img);

	vector<Dot*>nowplace;
	int first = 0; //记录当前页显示的首个景点在景点库中的下标
	changeThreePlace(first, nowplace); //先显示下标0到2的景点


	//持续循环直到获取鼠标点击按钮的信号
	while (1) {
		if (Window::havemsg()) {
			m_msg = Window::getmsg();
			if (m_msg.x >= 0 && m_msg.x <= 68 && m_msg.y >= 0 && m_msg.y <= 83) {
				if (m_msg.message == WM_LBUTTONDOWN) { //点击返回键，回到主页面
					return;
				}
			}

			if (m_msg.x >= 25 && m_msg.x <= 349 && m_msg.y >= 185 && m_msg.y <= 341) {
				if (m_msg.message == WM_LBUTTONDOWN) { //点击第一个景点卡片，进入详细信息
					placeShow(nowplace[0]);
				}
			}
			if (m_msg.x >= 25 && m_msg.x <= 349 && m_msg.y >= 361 && m_msg.y <= 517 && nowplace.size() >= 2) {
				if (m_msg.message == WM_LBUTTONDOWN) { //点击第二个景点卡片，进入详细信息
					placeShow(nowplace[1]);
				}
			}
			if (m_msg.x >= 25 && m_msg.x <= 349 && m_msg.y >= 537 && m_msg.y <= 693 && nowplace.size() >= 3) {
				if (m_msg.message == WM_LBUTTONDOWN) { //点击第三个景点卡片，进入详细信息
					placeShow(nowplace[2]);
				}
			}

			if (m_msg.x >= 260 && m_msg.x <= 331 && m_msg.y >= 719 && m_msg.y <= 785) {
				if (m_msg.message == WM_LBUTTONDOWN) { //点击向右翻页
					first = first + 3;//显示的景点为当前页的后三个
					if (first < SceneryKu.size()) {
						while (!nowplace.empty()) {
							nowplace.pop_back();
						}
						changeThreePlace(first, nowplace);
					}
					else { //无下一页
						first = first - 3;
					}
				}
			}
			if (m_msg.x >= 41 && m_msg.x <= 112 && m_msg.y >= 719 && m_msg.y <= 785) {
				if (m_msg.message == WM_LBUTTONDOWN) { //点击向左翻页
					first = first - 3;//显示的景点为当前页的前三个
					if (first >= 0) {
						while (!nowplace.empty()) {
							nowplace.pop_back();
						}
						changeThreePlace(first, nowplace);
					}
					else { //无前一页
						first = first + 3;
					}
				}
			}


			btn1.setmsg(m_msg);
			if (btn1.isCliked()) { //点击了查询按钮
				string pl = tb1.gettext();
				vector<Dot*>searchpl = InquireScenery(pl);
				while (!nowplace.empty()) {
					nowplace.pop_back();
				}
				changeSearchplace(0, nowplace, searchpl);
			}

			tb1.setmsg(m_msg); //景点名称输入
			if (tb1.isCliked()) {
				tb1.updatetext(0);
				putimage(25, 115, &fangdajing);
			}
		}
	}
}

//翻页修改的内容
void System::changeThreePlace(int first, vector<Dot*>& np)
{
	setfillcolor(RGB(255, 255, 255));
	fillroundrect(0, 185, 375, 693, 0, 0);//遮盖上一次显示
	fillroundrect(135, 769, 239, 812, 0, 0);//遮盖上一次显示

	//页码变化
	int nowpage = first / 3;
	nowpage++;//当前页码
	int allpage = (SceneryKu.size()) / 3;
	if (SceneryKu.size() / 3 != 0) {
		allpage++;
	}
	string showPage = " (";
	string nowp = to_string(nowpage);
	string allp = to_string(allpage);
	showPage.append(nowp);
	showPage.append("/");
	showPage.append(allp);
	showPage.append(") ");
	settextcolor(RGB(0, 72, 125));
	settextstyle(24, 0, "黑体");//设置文本大小，垂直压缩，字体
	setbkmode(TRANSPARENT);//去除文字背景
	outtextxy(139, 772, showPage.c_str());

	//景点图片，名称变化
	int i = 0;
	int textx = 213;
	int texty = 248;
	int picturex = 46;
	int picturey = 208;
	for (auto it = SceneryKu.begin(); it != SceneryKu.end() && i <= first + 2; it++) {
		if (i >= first && i <= first + 2) { //每页显示三个
			np.push_back(it->second);
			string placename = it->second->Getname();
			settextcolor(RGB(0, 0, 0));
			settextstyle(24, 0, "黑体");//设置文本大小，垂直压缩，字体
			setbkmode(TRANSPARENT);//去除文字背景
			outtextxy(textx, texty, placename.c_str());

			string picture = "./";
			picture.append(placename);
			picture.append(".png");
			IMAGE p;
			loadimage(&p, picture.c_str(), 131, 110);
			putimage(picturex, picturey, &p);

			//下一个显示向下移动
			texty = texty + 176;
			picturey = picturey + 176;
		}
		i++;
	}
}

//经过查询后修改的部分
void System::changeSearchplace(int first, vector<Dot*>& np, vector<Dot*>& searchpl)
{
	setfillcolor(RGB(255, 255, 255));
	fillroundrect(0, 185, 375, 693, 0, 0);//遮盖上一次显示
	fillroundrect(135, 769, 239, 812, 0, 0);//遮盖上一次显示

	//页码变化
	int nowpage = first / 3;
	nowpage++;//当前页码
	int allpage = (SceneryKu.size()) / 3;
	if (SceneryKu.size() / 3 != 0) {
		allpage++;
	}
	string showPage = " (";
	string nowp = to_string(nowpage);
	string allp = to_string(allpage);
	showPage.append(nowp);
	showPage.append("/");
	showPage.append(allp);
	showPage.append(") ");
	settextcolor(RGB(0, 72, 125));
	settextstyle(24, 0, "黑体");//设置文本大小，垂直压缩，字体
	setbkmode(TRANSPARENT);//去除文字背景
	outtextxy(139, 772, showPage.c_str());

	//景点图片，名称变化
	int i = 0;
	int textx = 213;
	int texty = 248;
	int picturex = 46;
	int picturey = 208;
	for (int i = 0; i < searchpl.size(); i++) {
		np.push_back(searchpl[i]);
		string placename = searchpl[i]->Getname();
		settextcolor(RGB(0, 0, 0));
		settextstyle(24, 0, "黑体");//设置文本大小，垂直压缩，字体
		setbkmode(TRANSPARENT);//去除文字背景
		outtextxy(textx, texty, placename.c_str());

		string picture = "./";
		picture.append(placename);
		picture.append(".png");
		IMAGE p;
		loadimage(&p, picture.c_str(), 131, 110);
		putimage(picturex, picturey, &p);

		//下一个显示向下移动
		texty = texty + 176;
		picturey = picturey + 176;
	}
}

//具体景点信息显示
void System::placeShow(Dot*& scen)
{
	setbkcolor(RGB(255, 255, 255));
	cleardevice();
	//顶栏设置
	setfillcolor(RGB(130, 226, 255));
	fillroundrect(0, 0, 375, 83, 0, 0);
	settextcolor(RGB(0, 72, 125));
	settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
	setbkmode(TRANSPARENT);//去除文字背景
	outtextxy(151, 20, "景点");
	IMAGE retn;
	loadimage(&retn, "./返回.png", 16, 29);
	putimage(25, 27, &retn);

	//景点图片
	string pname = "./";
	pname.append(scen->Getname());
	pname.append(".png");
	IMAGE placeP;
	loadimage(&placeP, pname.c_str(), 375, 314);
	putimage(0, 83, &placeP);

	//景点信息
	string name, num, address, introd;
	name = scen->Getname();
	int sceID = scen->GetID();
	num = to_string(sceID);

	introd = scen->GetImformation();

	setfillcolor(RGB(255, 255, 255));
	fillroundrect(0, 341, 375, 862, 35, 35);
	settextcolor(RGB(0, 0, 0));
	settextstyle(36, 0, "黑体");//设置文本大小，垂直压缩，字体
	setbkmode(TRANSPARENT);//去除文字背景
	outtextxy(25, 360, name.c_str());//景点名称

	settextcolor(RGB(0, 0, 0));
	settextstyle(32, 0, "黑体");//设置文本大小，垂直压缩，字体
	setbkmode(TRANSPARENT);//去除文字背景
	outtextxy(25, 415, num.c_str());//景点代号

	//景点地址
	outtextxy(25, 463, "地点:");
	outtextxy(121, 463, address.c_str());

	outtextxy(25, 511, "简介:");//景点简介
	string oneline;    //简介采用换行显示
	int linex = 25;
	int liney = 550 - 38;
	for (int i1 = 0, i2 = 0; i1 <= (introd.length()); i1++) {
		if (i1 % 20 == 0 && i1 != 0) { //截取出9个字符在输入框中显示为一行
			oneline = introd.substr(i2, i1);
			i2 = i1;
			liney = liney + 38;  //每行宽度为38
			outtextxy(linex, liney, oneline.c_str());
		}
		else if (i1 == introd.length()) { //最后一行不足10个字符
			oneline = introd.substr(i2, i1);
			liney = liney + 38;  //每行宽度为38
			outtextxy(linex, liney, oneline.c_str());
		}
		else {

		}
	}

	//下栏图标设置
	IMAGE img;
	loadimage(&img, "./景点.png", 44, 44);
	putimage(166, 748, &img);
	setlinecolor(RGB(15, 19, 174));
	line(27, 774, 145, 774);
	line(230, 774, 348, 774);

	//持续循环直到获取鼠标点击按钮的信号
	while (1) {
		if (Window::havemsg()) {
			m_msg = Window::getmsg();
			if (m_msg.x >= 0 && m_msg.x <= 68 && m_msg.y >= 0 && m_msg.y <= 83) {
				if (m_msg.message == WM_LBUTTONDOWN) { //点击返回键，回到景点搜索页面
					return;
				}
			}
		}
	}
}